package com.LeetCode.doublePair;

import org.junit.Test;

/**
 * 接雨水
 */
public class trap {
    public int trap(int[] height) {
        int l = 0, r = height.length-1;
        int leftMax = 0, rightMax = 0;
        int res = 0;
        while(l < r){
            if(height[l] < height[r]){
                leftMax = Math.max(leftMax , height[l]);
                res +=  leftMax - height[l];
                l++;
            }else{
                rightMax = Math.max(rightMax , height[r]);
                res +=  rightMax - height[r];
                r--;
            }
        }
        return res;
    }

    @Test
    public void testTrap(){
        int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
        System.out.println(trap(height));
    }
}
